<template>
    <div class="login_container">
        <div class="login_box">
            <div class="avatar_box">
                <img src="@/assets/logo.png" />
            </div>

            <el-form
                    ref="form"
                    :model="managerForm"
                    :rules="loginFormRules"
                    label-width="0px"
                    class="login_form"
            >
                <el-form-item prop="mCode">
                    <el-input prefix-icon="el-icon-user" placeholder="用户账号" v-model="managerForm.mCode"></el-input>
                </el-form-item>
                <el-form-item prop="mPassword">
                    <el-input
                            prefix-icon="el-icon-lock"
                            placeholder="密码"
                            v-model="managerForm.mPassword"
                            type="password"
                    ></el-input>
                </el-form-item>
                <el-form-item class="btns">
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button type="info" @click="resetLogin">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
        name: "Login",
        data(){
            return{
                managerForm:{
                    mCode:'',
                    mPassword:''
                },
                loginFormRules:{
                    mCode:[
                        {required:true,message:'用户名不能为空',trigger:'blur'}
                    ]
                }
            }
        },
        methods:{
            login(){
                this.$refs.form.validate(a=>{
                    if (!a) {
                        return ;
                    } else {
                        let url = this.$baseUrl+"manager/byCodeLogin";
                        let params = this.$qs.stringify(this.managerForm);//将json转成键值对的字符串
                        //发送axios请求
                        this.$axios.post(url,params).then((res)=>{
                            if (res.data.code==200){//后台登录验证通过
                                this.$message.success(res.data.msg);
                                sessionStorage.setItem("token",res.data.data);
                                this.$router.push("/home")
                            }else{
                                this.$message.error(res.data.msg);//登陆失败
                            }
                        });
                        this.$router.push("/home")
                    }
                })
            },
            resetLogin(){
                this.$refs.form.resetFields()
            }
        }
    }
</script>

<style lang="less" scoped>
    .login_container {
        background-color: #2b4b6b;
        height: 100%;
    }
    .login_box {
        width: 450px;
        height: 300px;
        background-color: #fff;
        border-radius: 3px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        .avatar_box {
            height: 100px;
            width: 100px;
            border-radius: 50%;
            border: 1px solid #eee;
            padding: 10px;
            box-shadow: 0 0 10px #ddd;
            position: absolute;
            left: 50%;
            transform: translate(-50%, -30%);
            background-color: #fff;
            img {
                width: 100%;
                height: 100%;
                border-radius: 50%;
                background-color: #eee;
            }
        }
    }
    .btns {
        display: flex;
        justify-content: center;
    }
    .login_form {
        position: absolute;
        bottom: 0;
        width: 100%;
        padding: 0 10px;
        box-sizing: border-box;
    }
</style>